package org.hit.burkun.util;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class GlobalData {

	private Properties properties;
	private String filePath;

	private GlobalData(String filePath) {
		this.filePath = filePath;
		properties = loadProperties(filePath);
	}

	// ===================================
	private Properties loadProperties(String filePath) {
		InputStream is;
		try {
			is = new BufferedInputStream(
					new FileInputStream(new File(filePath)));
			Properties properties = new Properties();
			properties.load(is);
			return properties;
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}

	public Properties getProperties() {
		return properties;
	}

	public void setPropertity(String key, String value) {
		FileOutputStream oFile;
		try {
			properties.setProperty(key, value);
			oFile = new FileOutputStream(filePath);
			properties.store(oFile, "add counter!");
			oFile.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public String getTempDir() {
		String path = properties.getProperty("TEMP_PATH");
		File dir = new File(path);
		if (!dir.exists()) {
			try {
				dir.mkdir();
			} catch (SecurityException e) {
				e.printStackTrace();
			}
		}
		return path;
	}

	public String getParserDir() {
		String path = properties.getProperty("PARSER_DIR");
		File dir = new File(path);
		if (!dir.exists()) {
			try {
				dir.mkdir();
			} catch (SecurityException e) {
				e.printStackTrace();
			}
		}
		return path;
	}

	// get api
	public String getStringInteractionPath() {
		String path = properties.getProperty("STRING_INTERACTION_COMBINE_PATH");
		return path;
	}

	public String getStringGeneIdPath() {
		String path = properties.getProperty("STRING_GENEID_MAPPING");
		return path;
	}

	public String getHgncPath() {
		String path = properties.getProperty("HGNC_COMPLETE_NEW");
		return path;
	}

	public String getGOPath() {
		String path = properties.getProperty("GO");
		return path;
	}

	public String getDOPath() {
		String path = properties.getProperty("DO");
		return path;
	}

	public String getHPOPath() {
		String path = properties.getProperty("HPO");
		return path;
	}

	public String getHpGenePath() {
		String path = properties.getProperty("HP_GENE");
		return path;
	}

	public String getGoaPath() {
		return properties.getProperty("GOA");
	}

	public String getOmim2GenePath() {
		return properties.getProperty("OMIM_GENE");
	}

	public String getOmimMorbidmapPath() {
		return properties.getProperty("OMIM_MORBIDMAP");

	}

	public String getCtdGeneDisease() {
		return properties.getProperty("CTD_GENE_DISEASE");
	}

	public String getGeneTermPath() {
		return properties.getProperty("GENE_TERM");
	}

	public String getHpoAnnotationPath() {
		return properties.getProperty("HPO_ANNOTATION");
	}

	public String getOmim2DoPath() {
		return properties.getProperty("OMIM2DO");
	}

	public String getMesh2DoPath() {
		return properties.getProperty("MESH2DO");
	}

	// ====================================
	public static String getFileName(String url) {
		return url.substring(url.lastIndexOf("/") + 1, url.length());
	}

	private static GlobalData instance = new GlobalData(
			"configure/settings.properties");

	public static GlobalData getInstance() {
		return instance;
	}

}
